(function($) {
    "use strict";
    var methods={
        update:function(args){

            var obj=$(this);
            var jp_before_m=obj.children(".before").children(".half-circle");
            var jp_after_m=obj.children(".after").children(".half-circle");
            var value=args;
            if(arguments[1]&&arguments[1]==true){
                var color=jp_after_m.css("borderTopColor");
                var span=obj.children(".nowvalue");
                if(span.length==0){
                    obj.append("<div class='nowvalue'><span></span></div>");
                    span=obj.children(".nowvalue").children("span");
                }else{
                    span=span.children('span');
                }
                span.css("color",color);
                span.html(value);
            }
            obj.attr('aria-valuenow',value);
            var min=obj.attr('aria-valuemin');
            if(min==null){
                min=0;
            }
            var max=obj.attr('aria-valuemax');
            if(max==null){
                max=100;
            }
            if(value<min){
                value=min;
            }
            if(value>max){
                value=max;
            }
            var deg=360*value/(max-min);
            if(deg<=180){
                jp_before_m.css('transform','rotate(135deg)');
                jp_after_m.css('transform','rotate('+(deg-135)+'deg)');
            }else{
                jp_after_m.css('transform','rotate('+(180-135)+'deg)');
                jp_before_m.css('transform','rotate('+(deg-180+135)+'deg)');
            }
        },
        init:function(){
            return this.each(function(){

                var obj=$(this);
                if(obj){
                    var width=obj.width();
                    var height=obj.height();


                    var jp_before=obj.children(".before");
                    var jp_after=obj.children(".after");
                    if(jp_before.length==0){
                        obj.append("<div class='before'></div>");
                        jp_before=obj.children(".before");
                    }
                    jp_before.width(width/2);
                    jp_before.css("left","0px");
                    jp_before.height(height);
                    if(jp_after.length==0){
                        obj.append("<div class='after'></div>");
                        jp_after=obj.children(".after");
                    }
                    jp_after.width(width/2);
                    jp_after.css("left",width/2+"px");
                    jp_after.height(height);
                    var jp_before_m=jp_before.children('.half-circle');
                    if(jp_before_m.length==0){
                        jp_before.append("<div class='half-circle'></div>");
                        jp_before_m=jp_before.children(".half-circle");
                    }
                    var border_width=parseInt(jp_before_m.css("borderTopWidth"));
                    jp_before_m.width(width-border_width*2);

                    jp_before_m.height(height-border_width*2);
                    var jp_after_m=jp_after.children('.half-circle');
                    if(jp_after_m.length==0){
                        jp_after.append("<div class='half-circle'></div>");
                        jp_after_m=jp_after.children(".half-circle");
                    }
                    jp_after_m.width(width-border_width*2);
                    jp_after_m.height(height-border_width*2);

                    var value=Number(obj.attr('aria-valuenow'));

                    var min=Number(obj.attr('aria-valuemin'));
                    if(min==null){
                        min=0;
                    }
                    var max=Number(obj.attr('aria-valuemax'));
                    if(max==null){
                        max=100;
                    }
                    if(value<min){
                        value=min;
                    }

                    if(value>max){
                        value=max;
                    }

                    var deg=360*value/(max-min);

                    if(deg<=180){
                        jp_before_m.css('transform','rotate(135deg)');
                        jp_after_m.css('transform','rotate('+(deg-135)+'deg)');

                    }else{
                        jp_after_m.css('transform','rotate('+(180-135)+'deg)');
                        jp_before_m.css('transform','rotate('+(deg-180+135)+'deg)');
                    }
                    jp_before_m.css('transition','transform 0.1s linear');
                    jp_after_m.css('transition','transform 0.1s linear');



                }

            });
        }
    };
    $.fn.pgcircular = function () {
        var method = arguments[0];
        if(methods[method]){

            if(!this.isJpCircleInit||this.isJpCircleInit==false){
                methods.init.apply(this, args);
                this.isJpCircleInit=true;

            }
            method=methods[method];
            var args = Array.prototype.slice.call(arguments, 1);
            return method.apply(this, args);
        }else{
            if(!this.isJpCircleInit||this.isJpCircleInit==false) {
                method = methods.init;
                this.isJpCircleInit=true;
                this.each(function(){
                    $.fn.isJpCircleInit=true;

                });
                return method.apply(this, args);
            }

        }


    }
}(jQuery));
